// const path = require('path')

// 获取数据库模型
const database = require('../modules/databse')

// 创建对象数据
module.exports.create = function (modelName, obj, cb) {
    let db = database.getDataBase()
    let Model = db.models[modelName]
    Model.create(obj, cb)
}

/**
 * 获取所有数据
 * 查询条件统一规范
 * conditions
	{
		"columns" : {
			字段条件
			"字段名" : "条件值"
		},
		"offset" : "偏移",
		"omit" : ["字段"],
		"only" : ["需要字段"],
		"limit" : "",
		"order" :[ 
			"字段" , A | Z,
			...
		]
	}
 */
module.exports.list = function(modelName,conditions,cb) {
        let db = database.getDataBase()
        let Model = db.models[modelName];

        if(!Model) return cb("模型不存在",null);

        if(conditions) {
            if(conditions["columns"]) {
                Model = Model.find(conditions["columns"]);
            } else {
                Model = Model.find();
            }
    
            if(conditions["offset"]) {
                Model = Model.offset(parseInt(conditions["offset"]));
            }
    
            if(conditions["limit"]) {
                Model = Model.limit(parseInt(conditions["limit"]));
            }
    
            if(conditions["only"]) {
                Model = Model.only(conditions["only"]);
            }
    
            if(conditions["omit"]) {
                Model = Model.omit(conditions["omit"]);
            }
    
            if(conditions["order"]) {
                Model = Model.order(conditions["order"]);
            }
        } else {
            Model = Model.find();
        }

        Model.run(function(err,models) {
            if(err) {
                return cb("查询失败",null);
            }
            cb(null,models);
        });
};

// 按条件count
module.exports.countByConditions = function (modelName, conditions, cb) {
    let db = database.getDataBase()
    let Model = db.models[modelName]
    if (!Model) {
        return cb('模型不存在', null)
    }
    const resultCallback= function (err, count) {
        if (err) {
            return cb('查询失败', null)
        }
        cb(null, count)
    }
    if (conditions) {
        if (conditions['columns']) {
            Model  = Model.count(conditions['columns'], resultCallback)
        } else {
            Model = Model.count(resultCallback)
        }
    } else {
        Model = Model.count(resultCallback)
    }
}

/**
 * 获取一条数据
 */
module.exports.findOne = function (modelName, conditions, cb) {
    let db = database.getDataBase()
    let Model = db.models[modelName]
    if (!Model) {
        return cb('模型不存在', null)
    }
    if (!conditions) return cb('条件为空', null)
    Model.one(conditions, function(err, obj) {
        if (err) {
            return cb('查询失败', null)
        }
        return cb(null, obj)
    })
}

/**
 * 更新数据
 */
module.exports.update = function (modelName, id , updateObj, cb) {
    let db = database.getDataBase()
    let Model = db.models[modelName]
    Model.get(id, function(err, obj) {
        if (err) return cb('更新失败', null)
        obj.save(updateObj,cb)
    })
}

/**
 * 根据主键ID获取对象
 */
module.exports.show = function (modelName, id, cb) {
    const db = database.getDataBase()
    const Model = db.models[modelName]
    Model.get(id, function(err, obj) {
        cb(err, obj)
    })
}

/**
 * 根据主键ID删除对象
 */
module.exports.destroy = function (modelName, id, cb) {
    const db = database.getDataBase()
    const Model = db.models[modelName]
    Model.get(id, function(err, obj) {
        if (err) return cb('无模型ID')
        obj.remove(function(err) {
            if (err) return cb('删除失败')
            return cb(null)
        })
    })
}

/**
 * 通过模型名称获取数据库数量
 */
module.exports.count = function(modelName,cb) {
	const db = database.getDatabase();
	const Model = db.models[modelName];
	Model.count(cb);
}

/**
 * 通过条件判断数据是否存在
 */
module.exports.exists = function(modelName,conditions,cb) {
	const db = database.getDatabase();
	const Model = db.models[modelName];
	Model.exists(conditions,function(err,isExists){
		if(err) return cb("查询失败");
		 cb(null,isExists);
	});
}

module.exports.getModel = function(modelName) {
    const Model = db.models[modelName];
    db = database.getDatabase();
	return db.models[modelName];
}